#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
  pid_t ids[10];

  for(int i = 0; i < 10; i++)
  {
    pid_t id = fork();
    if(id == 0){
      //child
      int count = 10;
      while(count > 0)
      {
        printf("child do something!: %d, %d\n", getpid(), getppid());
        sleep(1);
        count--;
      }
     // if(i == 4)
     // {
     //   int a;
     //   a = 1 / 0;
     // }

      if(i == 7) {
        int* pa;
        *pa = 100;
      }
      exit(i);
    }
    //father
    ids[i] = id;
  }

  int count = 0;
  while(count < 10)
  {
    int status = 0;
    pid_t ret = waitpid(ids[count], &status, 0);
    if(ret >= 0){
      printf("wait child successfully! pid:%d\n", ret);
      printf("status: %d\n", status);
      if(WIFEXITED(status)){
        printf("child exit code:%d\n", WEXITSTATUS(status));
      }
      else{
        printf("child not exit normal!\n");
      }
      //  printf("child exit code:%d\n", (status>>8)&0xff);
    //  printf("child get signal:%d\n", status&0x7f);
    }
    count++;
  }

  return 0;
}
